//
//  JKSpecialColumnViewController.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/14.
//  Copyright © 2018年 安永博. All rights reserved.
//  专栏

import UIKit

class JKSpecialColumnViewController: JKBaseViewController {
    
    /** 模型数组 */
    private var sectionLists = [JKNewsDetailSectionModel]()
    
    // MARK:- 生命周期方法
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
//        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        
    }

    // MARK: - 初始化
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }
    
    override func setupNav() {
        
        navTitle = "专栏"
    }
    
    override func setupTableView() {
        
        tableViewPlain.rowHeight = 66
        tableViewPlain.contentInset = UIEdgeInsetsMake(JKNavBarHeight, 0, JKCurrentHomeIndicatorHeight, 0)
        
        if #available(iOS 11.0, *) {
            
        } else {
            
            tableViewPlain.scrollIndicatorInsets = tableViewPlain.contentInset
        }
        
        tableViewPlain.register(UINib(nibName: "JKSpecialColumnCell", bundle: nil), forCellReuseIdentifier: "JKSpecialColumnCell")
    }
    
    // MARK: - 加载时间
    
    override func loadNewData() {
        
        JKNetworkingTool.shared.request(url: "http://news-at.zhihu.com/api/3/sections", method: .get, parameters: nil) { (responseJson, error) in
            
            if error != nil {
                
                self.showReloadView()
                
                return
            }
            
            guard let json = responseJson as? [String : AnyObject] else {
                
                self.showReloadView()
                
                return
            }
            
            guard let setionArr = (json["data"]) as? [[String: AnyObject]] else {
                
                self.showReloadView()
                
                return
            }
            
            guard let setionModelArr = [JKNewsDetailSectionModel].deserialize(from: setionArr) else {
                
                self.showReloadView()
                
                return
            }
            
            self.hideReloadView()
            
            self.sectionLists = setionModelArr as! [JKNewsDetailSectionModel]
            
            self.tableViewPlain.reloadData()
        }
    }

    
    // MARK: - 点击事件
    
    override func navBackButtonClick() {
        
        let mainNav = navigationController as! JKMainNavigationController
        
        if mainNav.showLeftVcBlock != nil {
            
            mainNav.showLeftVcBlock!()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JKSpecialColumnViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.sectionLists.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "JKSpecialColumnCell") as? JKSpecialColumnCell
        
        if cell == nil {
            
            cell = JKSpecialColumnCell(style: UITableViewCellStyle.default, reuseIdentifier: "JKSpecialColumnCell")
        }
        
        cell!.model = self.sectionLists[indexPath.row]
        
        return cell!
    }
}

extension JKSpecialColumnViewController {
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let vc = JKSectionStoryListViewController()
        
        vc.section = sectionLists[indexPath.row]
        
        navigationController?.pushViewController(vc, animated: true)
    }
}
